package dyy.zc.sportruler.ui.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import dyy.zc.sportruler.R;
import dyy.zc.sportruler.application.MyApplication;
import dyy.zc.sportruler.base.BaseActivity;
import dyy.zc.sportruler.utils.BmobUtils;
import dyy.zc.sportruler.utils.ToastUtils;

public class JianjieActivity extends BaseActivity {

    private TextView title,save;
    private EditText jianjie;
    private BmobUtils bmobUtils;
    @Override
    protected void findViewByIDS() {
        title = findViewsById(R.id.top_tv_title);
        save = findViewsById(R.id.save);
        jianjie = findViewsById(R.id.et_jianjie);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jianjie);
        initData();
        bind();
    }

    private void bind() {
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String content = jianjie.getText().toString();
                if (content.length()>150){
                    ToastUtils.shortToast(JianjieActivity.this,"内容字数超过150个字");
                    return;
                }
                dialog = ProgressDialog.show(JianjieActivity.this,null,"正在保存信息......");
                loginuser.setContent(content);
                bmobUtils.updateInfo(loginuser,1,handler);
            }
        });
    }

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:
                    MyApplication.getInstance().saveUserInfo(loginuser);
                    dialog.dismiss();
                    Intent intent = new Intent();
                    intent.putExtra("login",loginuser);
                    setResult(123,intent);
                    finish();
                    break;
            }
        }
    };

    private void initData() {
        if (loginuser.getContent() != null){
            if (!loginuser.getContent().equals(""))
                 jianjie.setText(loginuser.getContent());
        }
        title.setText("个人简介");
        bmobUtils = new BmobUtils(this);
    }
}
